# Maintainer: Richard Shaw <hobbes1069@gmail.com>

_realname=fltk
pkgbase=mingw-w64-${_realname}1.3
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}1.3"
pkgver=1.3.11
pkgrel=2
pkgdesc="C++ user interface toolkit (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.fltk.org/"
license=('spdx:LGPL-2.0-or-later WITH FLTK-exception')
depends=("${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
source=("https://github.com/${_realname}/${_realname}/archive/release-${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-cmake-fix-install.patch"
        "0002-fltk-config-uuid-shared.patch")
sha256sums=('2378a965971eaa7325decc7b48deef9ab666c7fdb4c244fcd95fce0ecdddc6c3'
            'd6a882f3645f7de96215ca9cc666a1239781d2d03f2f5266b452eed67f577506'
            'c20691e1a11994d0d7d681751f1812abd5dfeca7550293d1edacb0c88f62a03c')

prepare() {
  cd "${srcdir}/${_realname}-release-${pkgver}"

  patch -p1 -i "${srcdir}/0001-cmake-fix-install.patch"
  patch -p1 -i "${srcdir}/0002-fltk-config-uuid-shared.patch"
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CXXFLAGS+=" -Wno-c++11-narrowing"
  else
    CXXFLAGS+=" -Wno-narrowing"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -Wno-dev \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "${_extra_config[@]}" \
    -DFLTK_BUILD_TEST=OFF \
    -DOPTION_BUILD_SHARED_LIBS=ON \
    "../${_realname}-release-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/share/fltk/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/bin/fltk-config"

  install -Dm644 "${srcdir}/${_realname}-release-${pkgver}/COPYING" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
